package xrz.cuanbao.views;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import xrz.cuanbao.R;

public class EmjoyView extends LinearLayout{

	private ViewPager viewPager;
	private EmjoyPagerAdapter pagerAdapter;
	public EmjoyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public EmjoyView(Context context) {
		super(context);
		init();
	}

	private void init(){
		LinearLayout layout = new LinearLayout(getContext());
		layout.setOrientation(HORIZONTAL);
		
		this.setOrientation(VERTICAL);
		viewPager = new ViewPager(getContext());
		int pagesize = EmjoyUtils.getAllPage();
		List<GridView> list = new ArrayList<GridView>();
		for(int i=0;i<pagesize;i++){
			final GridView gridView = new GridView(getContext());
			gridView.setNumColumns(7);
			gridView.setTag(i);
			gridView.setVerticalSpacing(15);
			EmjoyGridViewAdapter adapter = new EmjoyGridViewAdapter(getContext(), i);
			gridView.setAdapter(adapter);
			list.add(gridView);
			gridView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					int page = (Integer) gridView.getTag();
					String resname = EmjoyUtils.getResName(page, position);
					if(listener != null)
						listener.onEmjoyClickListener(resname);
				}
			});
			
			ImageView imageView = new ImageView(getContext());
			imageView.setPadding(5, 2, 5, 2);
			imageView.setImageResource(i == 0 ? R.drawable.select_dot : R.drawable.normal_dot);
			layout.addView(imageView);
		}
		pagerAdapter = new EmjoyPagerAdapter(list);
		viewPager.setAdapter(pagerAdapter);
		this.addView(viewPager);
		this.addView(layout);
	}
	
	private OnEmjoyItemClickListener listener;
	public void setOnEmjoyItemClickListener(OnEmjoyItemClickListener listener){
		this.listener = listener;
	}
	public interface OnEmjoyItemClickListener{
		void onEmjoyClickListener(String resname);
	}
}
